//
//  @ Project : Adamantium Project
//  @ File Name : UI.cpp
//  @ Date : 12/8/2012
//  @ Author : 
//
//


#include "UI.h"

void UI::initBtn(BUTTON btn[6]) {
	/*
	NOTE:This function is not meant to add more buttons or whatso ever, 
	only existed to easy the reading and organization of the code
	*/
	// How Many Preset Positions of the button
	V2DF btnPos[size];

	// Decrementing Buttons
	btnPos[0] = V2DF(1035, 106);
	btnPos[1] = V2DF(1035, 299);
	btnPos[2] = V2DF(1035, 490);

	// Incrementing Buttons
	btnPos[3] = V2DF(1104, 106);
	btnPos[4] = V2DF(1104, 299);
	btnPos[5] = V2DF(1104, 490);

	GUAGE_TYPE type[size] = {Phaser, Missle, Shield, 
							 Phaser, Missle, Shield};
	for(int i = 0; i < size; i++) {
		if(i < 3) {
			btn[i].texture.initTexture( m_core,
				"sprites/UI/DecrementBtn.png",
				15, 
				btnPos[i], 
				V2D_ONES,
				D3DXToRadian(0.0f));
			btn[i].whatKindOfButton = Decrement;	// Decreasing button
			btn[i].whatKindOfEnergy = type[i];
		}
		else if(i > 2 && i < size){
			btn[i].texture.initTexture( m_core,
				"sprites/UI/IncrementBtn.png",
				15, 
				btnPos[i], 
				V2D_ONES,
				D3DXToRadian(0.0f));

			btn[i].whatKindOfButton = Increment;	// Increasing button
			btn[i].whatKindOfEnergy = type[i];
		}
		btn[i].texture.update();
		btn[i].click.m_core = m_core;
		btn[i].click.TOP   = btn[i].texture.m_pos.y;
		btn[i].click.DOWN  = btn[i].texture.m_pos.y + 42;
		btn[i].click.LEFT  = btn[i].texture.m_pos.x;
		btn[i].click.RIGHT = btn[i].texture.m_pos.x + 42;
	}	
}
void UI::init(BeanieCore* a_core) {
	m_core = a_core;

	Current_Day = 0;
	Ship_Status = 0;
	toDraw = new char[256];

	background.initTexture(	m_core,
		"sprites/UI/TheBackground.png",
		TEX_CENTER,
		V2DF(640, 360), 
		V2D_ONES,
		D3DXToRadian(0.0f));
	background.update();

	status.initTexture(	m_core,
		"sprites/UI/StatusBoxTrans.png",
		15, 
		V2DF(15,15), 
		V2D_ONES,
		D3DXToRadian(0.0f));
	status.update();

	energy_H.initTexture( m_core,
		"sprites/UI/EnergyBar_HorizontalTrans.png",
		30, 
		V2DF(30,65), 
		V2D_ONES,
		D3DXToRadian(0.0f));
	energy_H.update();


	panel[0].initTexture( m_core,
		"sprites/UI/PanelTrans.png",
		15, 
		V2DF(980,15), 
		V2D_ONES,
		D3DXToRadian(0.0f));
	panel[0].update();
	panel[1].initTexture( m_core,
		"sprites/UI/PanelTrans.png",
		15, 
		V2DF(980,207), 
		V2D_ONES,
		D3DXToRadian(0.0f));
	panel[1].update();
	panel[2].initTexture( m_core,
		"sprites/UI/PanelTrans.png",
		15, 
		V2DF(980,399), 
		V2D_ONES,
		D3DXToRadian(0.0f));
	panel[2].update();

	energy_V[0].initTexture( m_core,
		"sprites/UI/EnergyBar_VerticalTrans.png",
		15, 
		V2DF(1200,26), 
		V2D_ONES,
		D3DXToRadian(0.0f));
	energy_V[0].update();
	energy_V[1].initTexture( m_core,
		"sprites/UI/EnergyBar_VerticalTrans.png",
		30, 
		V2DF(1200,413), 
		V2D_ONES,
		D3DXToRadian(0.0f));
	energy_V[1].update();

	initBtn(btn);

	PhaserShieldBar[0].a = new char[256];	
	PhaserShieldBar[0].preset_X = 1202;
	PhaserShieldBar[0].preset_Y = 0;
	PhaserShieldBar[0].X = 0;
	PhaserShieldBar[0].Y = 170;
	//PhaserShieldBar[0].counter = 0;
	PhaserShieldBar[0].testCounter = 0;

	PhaserShieldBar[1].a = new char[256];	
	PhaserShieldBar[1].preset_X = 1202;
	PhaserShieldBar[1].preset_Y = 0;
	PhaserShieldBar[1].X = 0;
	PhaserShieldBar[1].Y = 556;
	//PhaserShieldBar[1].counter = 0;
	PhaserShieldBar[1].testCounter = 0;

	m.a = new char[256];
	//m.Max = 3;
	m.Count = 0;

	EnergyBar.a = new char[256];	
	EnergyBar.preset_X = 0;
	EnergyBar.preset_Y = 67;
	EnergyBar.X = 30;
	EnergyBar.Y = 0;
	EnergyBar.counter = 0;
	EnergyBar.testCounter = 0;
}

void UI::EnergyUsage(BUTTON a_btn) {	
	if(a_btn.whatKindOfButton == Increment) {
		if(a_btn.whatKindOfEnergy == Phaser) { 
			if (m_status->laserEn >= 99) return;
			if (m_status->laserEn < 100 && m_status->laserEn >= 0) {				
				if(m_core->keyInput(DIK_LSHIFT) || m_core->keyInput(DIK_RSHIFT)) {
					PhaserShieldBar[0].testCounter = m_status->laserEn+10;
					if(PhaserShieldBar[0].testCounter > 99) {
						PhaserShieldBar[0].testCounter = 99;
					}
					for(int i = m_status->laserEn; i < PhaserShieldBar[0].testCounter; i++) {
						PhaserShieldBar[0].position = V2DF(1202, --PhaserShieldBar[0].Y);
						PhaserShieldBar[0].bars[m_status->laserEn].initTexture( m_core,
							"sprites/UI/PhaserBar.png",
							15, 
							PhaserShieldBar[0].position, 
							V2D_ONES,
							D3DXToRadian(0.0f));
						PhaserShieldBar[0].bars[m_status->laserEn].update();
						m_status->laserEn++;
					}
				}
				else {
					PhaserShieldBar[0].position = V2DF(1202, --PhaserShieldBar[0].Y);
					PhaserShieldBar[0].bars[m_status->laserEn].initTexture( m_core,
						"sprites/UI/PhaserBar.png",
						15, 
						PhaserShieldBar[0].position, 
						V2D_ONES,
						D3DXToRadian(0.0f));
					PhaserShieldBar[0].bars[m_status->laserEn].update();

					m_status->laserEn++;
				}
			}
			return;
		}
		if(a_btn.whatKindOfEnergy == Shield) { 
			if (m_status->shieldEn >= 99) return;
			if (m_status->shieldEn < 100 && m_status->shieldEn >= 0) {			
				if(m_core->keyInput(DIK_LSHIFT) || m_core->keyInput(DIK_RSHIFT)) {
					PhaserShieldBar[1].testCounter = m_status->shieldEn+10;
					if(PhaserShieldBar[1].testCounter > 99) {
						PhaserShieldBar[1].testCounter = 99;
					}
					for(int i = m_status->shieldEn; i < PhaserShieldBar[1].testCounter; i++) {
						PhaserShieldBar[1].position = V2DF(1202, --PhaserShieldBar[1].Y);
						PhaserShieldBar[1].bars[m_status->shieldEn].initTexture( m_core,
							"sprites/UI/ShieldBar.png",
							15, 
							PhaserShieldBar[1].position, 
							V2D_ONES,
							D3DXToRadian(0.0f));
						PhaserShieldBar[1].bars[m_status->shieldEn].update();
						m_status->shieldEn++;
					}
				}
				else {
					PhaserShieldBar[1].position = V2DF(1202, --PhaserShieldBar[1].Y);
					PhaserShieldBar[1].bars[m_status->shieldEn].initTexture( m_core,
						"sprites/UI/ShieldBar.png",
						15, 
						PhaserShieldBar[1].position, 
						V2D_ONES,
						D3DXToRadian(0.0f));
					PhaserShieldBar[1].bars[m_status->shieldEn].update();

					m_status->shieldEn++;
				}
			}
			return;
		}
		
		if(a_btn.whatKindOfEnergy = Missle) {
			if(m_status->missleCount != 0) {
				++m.Count;
				--m_status->missleCount;
				return;
			}
			else
				return;
		}		
	}
	if(a_btn.whatKindOfButton == Decrement) {		
		if(a_btn.whatKindOfEnergy == Phaser) { 
			if (m_status->laserEn <= 0) return;
			if (m_status->laserEn <= 100) {
				if(m_core->keyInput(DIK_LSHIFT) || m_core->keyInput(DIK_RSHIFT)) {
					PhaserShieldBar[0].testCounter = m_status->laserEn-10;
					if(PhaserShieldBar[0].testCounter < 0) {
						PhaserShieldBar[0].testCounter = 0;
					}
					for(int i = m_status->laserEn; i > PhaserShieldBar[0].testCounter; i--) {
						PhaserShieldBar[0].position = V2DF(1202, ++PhaserShieldBar[0].Y);
						PhaserShieldBar[0].bars[m_status->laserEn].initTexture( m_core,
							"sprites/UI/PhaserBar.png",
							15, 
							PhaserShieldBar[0].position, 
							V2D_ONES,
							D3DXToRadian(0.0f));
						PhaserShieldBar[0].bars[m_status->laserEn].update();
						m_status->laserEn--;
					}
				}
				else {
					PhaserShieldBar[0].position = V2DF(1202, ++PhaserShieldBar[0].Y);
					PhaserShieldBar[0].bars[m_status->laserEn].initTexture( m_core,
						"sprites/UI/PhaserBar.png",
						15, 
						PhaserShieldBar[0].position, 
						V2D_ONES,
						D3DXToRadian(0.0f));
					PhaserShieldBar[0].bars[m_status->laserEn].update();
					m_status->laserEn--;
				}
			}
			return;
		}
		if(a_btn.whatKindOfEnergy == Shield) { 
			if (m_status->shieldEn <= 0) return;
			if (m_status->shieldEn <= 100) {			
				if(m_core->keyInput(DIK_LSHIFT) || m_core->keyInput(DIK_RSHIFT)) {
					PhaserShieldBar[1].testCounter = m_status->shieldEn-10;
					if(PhaserShieldBar[1].testCounter < 0) {
						PhaserShieldBar[1].testCounter = 0;
					}
					for(int i = m_status->shieldEn; i > PhaserShieldBar[1].testCounter; i--) {
						PhaserShieldBar[1].position = V2DF(1202, ++PhaserShieldBar[1].Y);
						PhaserShieldBar[1].bars[m_status->shieldEn].initTexture( m_core,
							"sprites/UI/ShieldBar.png",
							15, 
							PhaserShieldBar[1].position, 
							V2D_ONES,
							D3DXToRadian(0.0f));
						PhaserShieldBar[1].bars[m_status->shieldEn].update();
						m_status->shieldEn--;
					}
				}
				else {
					PhaserShieldBar[1].position = V2DF(1202, ++PhaserShieldBar[1].Y);
					PhaserShieldBar[1].bars[m_status->shieldEn].initTexture( m_core,
						"sprites/UI/ShieldBar.png",
						15, 
						PhaserShieldBar[1].position, 
						V2D_ONES,
						D3DXToRadian(0.0f));
					PhaserShieldBar[1].bars[m_status->shieldEn].update();
					m_status->shieldEn--;
				}
			}
			return;
		}

		if(a_btn.whatKindOfEnergy = Missle) {
			if(m_status->missleCount >= 0 && m_status->missleCount < 3) {
				--m.Count;
				++m_status->missleCount;
				return;
			}
			else
				return;
		}
	}
}

void UI::update() {
	// GIVIGN the proper action for each type of buttons
	for(int i = 0; i < size; i++) {
		if(btn[i].click.isClicked(BTN_LEFT)) {	
			EnergyUsage(btn[i]);
		}
	}
	// Checking Ship STATUS
	Check_Ship(Ship_Status);

	if(Current_Day < 0.0f)
		Current_Day = 0.0f;
	// TESTING Days
	if(m_core->keyPress(DIK_DOWNARROW))
		if(Current_Day > 0.0f)
			Current_Day -= 0.1f;
		else return;
	
	if(m_core->keyPress(DIK_UPARROW))
		Current_Day += 0.1f;

	if(m_core->keyPress(DIK_RIGHTARROW)) {
		if(EnergyBar.counter < 200) {
			EnergyBar.position = V2DF((EnergyBar.X += 2), EnergyBar.preset_Y);
			EnergyBar.bars[EnergyBar.counter].initTexture( m_core,
				"sprites/UI/EnergyBar.png",
				15, 
				EnergyBar.position, 
				V2D_ONES,
				D3DXToRadian(0.0f));
			EnergyBar.bars[EnergyBar.counter].update();
			if(EnergyBar.counter != 199)
				++EnergyBar.counter;
		}
		else return;
	}
	if(m_core->keyPress(DIK_LEFTARROW)) {
		if(EnergyBar.counter > 0) {
			EnergyBar.position = V2DF((EnergyBar.X -= 2), 30);
			EnergyBar.bars[EnergyBar.counter].initTexture( m_core,
				"sprites/UI/EnergyBar.png",
				15, 
				EnergyBar.position, 
				V2D_ONES,
				D3DXToRadian(0.0f));
			EnergyBar.bars[EnergyBar.counter].update();
			--EnergyBar.counter;
		}
		else
			return;
	}	
}

void UI::draw() {
	background.draw();
	background.m_rot += 0.001f;
	background.update();

	status.draw();
	m_core->drawText("ENRGY LEVEL: ", V2DF(30, 30), 0xFFff0000);

	m_core->drawText("CURRENT STATUS:", V2DF(30, 130), 0xFFff0000 );

	m_core->drawText("WarpDrive: " , V2DF(50, 150), 0xFFff0000);
	if(Ship_Status == GOOD) {
		m_core->drawText("GOOD" , V2DF(140, 150), Ship_TEXT_COLOR);
	}
	if(Ship_Status == DAMAGED) {
		m_core->drawText("DAMAGED" , V2DF(140, 150), Ship_TEXT_COLOR);
	}
	if(Ship_Status == BROKEN) {
		m_core->drawText("BROKEN" , V2DF(140, 150), Ship_TEXT_COLOR);
	}
	

	m_core->drawText("Days: " , V2DF(50, 170), 0xFFff0000);
	sprintf_s(toDraw, 256, "%4.1f", Current_Day);
	m_core->drawText(toDraw , V2DF(100, 170), 0xFFffff00);

	energy_H.draw();

	panel[0].draw();
	panel[1].draw();
	panel[2].draw();

	energy_V[0].draw();
	energy_V[1].draw();

	for(int i = 0; i < size; i++) {
		btn[i].texture.draw();
	}
	for(int i = 0; i < m_status->laserEn; i++) 
		PhaserShieldBar[0].bars[i].draw();

	for(int i = 0; i < m_status->shieldEn; i++) 
		PhaserShieldBar[1].bars[i].draw();

	for(int i = 0; i < EnergyBar.counter; i++) {
		EnergyBar.bars[i].draw();
	}

	sprintf_s(PhaserShieldBar[0].a, 256,"PHASER ENERGY: %d%c", m_status->laserEn, 37);
	m_core->drawText(PhaserShieldBar[0].a, V2DF(1000, 30), 0xFFff0000);

	sprintf_s(PhaserShieldBar[1].a, 256,"SHIELD ENERGY: %d%c", m_status->shieldEn, 37);
	m_core->drawText(PhaserShieldBar[1].a, V2DF(1000, 415), 0xFFff0000);

	sprintf_s(m.a, 256,"MISSILE: %d", m.Count);
	m_core->drawText(m.a, V2DF(1000, 222), 0xFFff0000);

	sprintf_s(m.a, 256,"Stock: %d", m_status->missleCount);
	m_core->drawText(m.a, V2DF(1185, 355), 0xFFff0000);

	sprintf_s(m.a, 256,"FPS: %d", m_core->getFPS());
	m_core->drawText(m.a, V2DF(1200, 700), 0xFFff0000);
}

void UI::setPlayerStatus(PlayerStatus* a_status) {
	m_status = a_status;
}

void UI::release() {
}

void UI::Check_Ship(int &status) {
	if(m_core->keyPress(DIK_SPACE) && m_core->keyInput(DIK_SPACE)) {
		Ship_Status += 1;
		if(Ship_Status > 2)
			Ship_Status = 0;
	}

	if(Ship_Status == GOOD) {
		Ship_TEXT_COLOR = 0xFF00ff00;
	}
	if(Ship_Status == DAMAGED) {
		Ship_TEXT_COLOR = 0xFFffff00;
	}
	if(Ship_Status == BROKEN) {
		Ship_TEXT_COLOR = 0xFFff0000;
	}
}